package com.alks.function.controller.biocontrol.deptwork;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.PcSupplementStatus;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.biocontrol.deptwork.SupTraceRequest;
import com.alks.function.data.vo.biocontrol.deptwork.SupTraceExcelVo;
import com.alks.function.service.biocontrol.deptwork.ISuppTraceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.alks.entity.data.enums.ResultCodeEnum.DEFAULT;

@RestController
@ApiRestController("/bioDeptWrok")
@Api(value = "生控作业", tags = "生控部-生控作业-补单追踪作业")
@RequiredArgsConstructor
@RoleCheck(prgIds = "140110")
public class SuppTraceController {
    private final ISuppTraceService service;

    @PutMapping("/getSupTraceQuery")
    @ApiOperation(value = "补单追踪-查询", tags = "生控部-生控作业-补单追踪作业")
    public ResponseInfo getSupTraceQuery(@RequestBody SupTraceRequest request) {
        return service.getSupTraceQuery(request);
    }

    @PutMapping("/supTraceUpdate")
    @ApiOperation(value = "补单追踪修改-更新", tags = "生控部-生控作业-补单追踪作业")
    public ResponseInfo supTraceUpdate(@RequestBody PcSupplementStatus request) {
        return service.supTraceUpdate(request);
    }

    @PostMapping("/supTraceDelete")
    @ApiOperation(value = "补单追踪删除-删除", tags = "生控部-生控作业-补单追踪作业")
    public ResponseInfo supTraceDelete(@RequestParam @ApiParam("补料单号") String slipNo,
                                       @RequestParam @ApiParam("指令") String lotNo) {
        return service.supTraceDelete(slipNo, lotNo);
    }


    @PutMapping("/getSupTraceExprod")
    @ApiOperation(value = "补单追踪导出-导出EXCEL", tags = "生控部-生控作业-补单追踪作业")
    public ResponseInfo getSupTraceExprod(@RequestBody SupTraceRequest request, HttpServletResponse response) {
        request.setPageSize(-1);
        /*抽取数据*/
        List<SupTraceExcelVo> vo = service.getSupTraceExprod(request);
        /*模版和生成文件的名称*/
        String templateName = "补单追踪模版";
        String excelName = "补单追踪_"+DateUtils.dateTimeNow();
        try {
            ExcelUtils.creatByTemplate(response,templateName,excelName,vo);
        } catch (IOException e) {
            return ResponseInfo.error("文件导出功能异常", DEFAULT.getCode());
        }
        return null;
    }
}
